/*
 * Created on Jul 18, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package dbUtil;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;

//import java.sql.*;
/**
 * @author Shengru Tu
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DBMySQL_Connection {

	private String dbURL;
	final String mySQLProtocol = "jdbc:mysql";

	/**
	 * @param host -- the database server url.
	 * @param port -- the database port number.
	 * @param sID --  the database name (SID).
	 */
	public DBMySQL_Connection (String host, int port, String sID) { 
		int portNum = 3306; 
		if (port != 0) portNum = port; 
		this.dbURL = mySQLProtocol + "://" + host + ":" + portNum + "/" + sID;
	}
	
	/**
	 * @param username -- the user name to access the database.
	 * @param password -- This constructor establishes connection.
	 */
	public Connection getDBConnection(String username, String password) {
		Connection conn = null; 
		// register the JDBC driver. 
		try {
			Class.forName("com.mysql.jdbc.Driver"); 
		} catch (Exception e) { 
			e.printStackTrace(); 
		}
		// Create the connection 
		try {
			conn = DriverManager.getConnection(dbURL, username, password);
		} catch (SQLException sqle) {
			sqle.printStackTrace(); 
		}
		return conn;
	}
}
